/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.overall.service.impl;

import me.zhengjie.modules.overall.domain.SysGrade;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.overall.repository.SysGradeRepository;
import me.zhengjie.modules.overall.service.SysGradeService;
import me.zhengjie.modules.overall.service.dto.SysGradeDto;
import me.zhengjie.modules.overall.service.dto.SysGradeQueryCriteria;
import me.zhengjie.modules.overall.service.mapstruct.SysGradeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wzy
* @date 2024-11-28
**/
@Service
@RequiredArgsConstructor
public class SysGradeServiceImpl implements SysGradeService {

    private final SysGradeRepository sysGradeRepository;
    private final SysGradeMapper sysGradeMapper;

    @Override
    public PageResult<SysGradeDto> queryAll(SysGradeQueryCriteria criteria, Pageable pageable){
        Page<SysGrade> page = sysGradeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sysGradeMapper::toDto));
    }

    @Override
    public List<SysGradeDto> queryAll(SysGradeQueryCriteria criteria){
        return sysGradeMapper.toDto(sysGradeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SysGradeDto findById(Integer stuid) {
        SysGrade sysGrade = sysGradeRepository.findById(stuid).orElseGet(SysGrade::new);
        ValidationUtil.isNull(sysGrade.getStuid(),"SysGrade","stuid",stuid);
        return sysGradeMapper.toDto(sysGrade);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SysGrade resources) {
        sysGradeRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysGrade resources) {
        SysGrade sysGrade = sysGradeRepository.findById(resources.getStuid()).orElseGet(SysGrade::new);
        ValidationUtil.isNull( sysGrade.getStuid(),"SysGrade","id",resources.getStuid());
        sysGrade.copy(resources);
        sysGradeRepository.save(sysGrade);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer stuid : ids) {
            sysGradeRepository.deleteById(stuid);
        }
    }

    @Override
    public void download(List<SysGradeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysGradeDto sysGrade : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程号", sysGrade.getClassid());
            map.put("考勤成绩", sysGrade.getKaoqin());
            map.put("作业成绩", sysGrade.getHomework());
            map.put("回答问题成绩", sysGrade.getReplycount());
            map.put("任务点成绩", sysGrade.getTask());
            map.put("总成绩", sysGrade.getOverall());
            map.put("姓名", sysGrade.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}